package com.sun.test.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhangquanwei
 * @version 1.0
 * @date 2024-12-18 18:55
 */
@Configuration
public class SpringDocConfig {
    private static final String BASE_PACKAGE = "com.sun.test.controller";
    private static final String API_TOKEN = "token";


    @Bean
    public GroupedOpenApi openApi(){
        return GroupedOpenApi.builder()
                .group("Api")
                .addOperationCustomizer((operation, handlerMethod) -> {
                    operation.addSecurityItem(new SecurityRequirement().addList(API_TOKEN));
                    return operation;
                })
                .packagesToScan(BASE_PACKAGE)
                .build();
    }

    @Bean
    public OpenAPI clientApi() {
        Components components = new Components();
        components.addSecuritySchemes(API_TOKEN,
                new SecurityScheme()
                        .type(SecurityScheme.Type.APIKEY)
                        .scheme("basic")
                        .name(API_TOKEN)
                        .in(SecurityScheme.In.HEADER)
                        .description("请求头")
        );
        return new OpenAPI().components(components).info(apiInfo());
    }

    private Info apiInfo() {
        Contact contact = new Contact();
        contact.setEmail("2214382071@qq.com");
        contact.setName("sun");
        contact.setUrl("https://little.cn");
        return new Info()
                .title("Swagger文档")
                .version("2.0")
                .contact(contact)
                .license(new License().name("Apache 2.0").url("http://springdoc.org"));
    }

}
